package com.it.test1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    static int ticket = 1000;
    static Lock lock = new ReentrantLock();
    public void run() {
        while (true) {
            lock.lock();
            try {
                if(ticket > 0){
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket--;
                    System.out.println(getName()+"卖了一张票!!!"+"，还剩"+ticket+"张票!!!");
                }else{
                    System.out.println(getName()+"卖完了票");
                    break;
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
